package LinkedList;

public class DoubleLinkedListDemo {
    public static void main(String[] args) {
        System.out.println("双向链表测试");
        //测试一把
        HeroNode2 hero1 = new HeroNode2(1,"亚信","第一场");
        HeroNode2 hero2 = new HeroNode2(2,"华讯","第二场");
        HeroNode2 hero3 = new HeroNode2(3,"富士通","第三场");
        HeroNode2 hero4 = new HeroNode2(4,"绿盟","第四场");
        HeroNode2 hero5 = new HeroNode2(5,"字节跳动","第五场");
        HeroNode2 hero6 = new HeroNode2(6,"烽火星空","第六场");

        //创建链表
        DoubleLinkedList DoubleLinkedList = new DoubleLinkedList();
        DoubleLinkedList.add(hero1);
        DoubleLinkedList.add(hero2);
        DoubleLinkedList.add(hero3);

        DoubleLinkedList.addByOrder(hero6);
        DoubleLinkedList.addByOrder(hero4);
        //DoubleLinkedList.addByOrder(hero3);

        //显示一把
        DoubleLinkedList.list();
    }
}
class DoubleLinkedList{
    //先初始化一个头节点，头结点不动
    private HeroNode2 head = new HeroNode2(0,"","");

    public HeroNode2 getHead() {
        return head;
    }

    public void list(){
        if (head.next == null){
            System.out.println("改链表是空的");
            return;
        }
        HeroNode2 temp = head.next;
        while (true){
            if (temp == null){
                break;
            }
            //输出节点信息
            System.out.println(temp);
            temp = temp.next;//temp后移
        }

    }
    public void add(HeroNode2 heroNode2){
        HeroNode2 temp = head;
        while(true){
            if (temp.next == null){
                break;
            }
            temp = temp.next;
        }
        temp.next = heroNode2;
        heroNode2.pre = temp;
    }
    public void  del(int no){
        if (head.next == null){
            System.out.println("链表是空的");
            return;
        }
        HeroNode2 temp = head.next;
        boolean flag = false;
        while(true){
            if (temp.no == no){
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag){
            temp.pre.next = temp.next;
            if (temp.next!=null){
                temp.next.pre = temp.pre;
            }
        }else {
            System.out.printf("要删的节点 %d 没找到",no);
        }
    }
    public void update(HeroNode2 newheroNode2){

        HeroNode2 temp = head.next;
        boolean flag = false;
        if (head.next == null){
            System.out.println("链表为空！！！");
            return;
        }
        while (true){
            if (temp.no == newheroNode2.no){
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag){
            temp.name = newheroNode2.name;
            temp.nickname = newheroNode2.nickname;
        }else {
            System.out.printf("没找到编号为 %d 的英雄\n",newheroNode2.no);
        }
    }

    public void addByOrder(HeroNode2 hero6) {
        HeroNode2 temp = head;
        boolean flag = false;
        while (true){
            if (temp.next == null){
                break;
            }
            if (temp.next.no == hero6.no){
                flag = true;
                break;
            }
            if (temp.next.no>hero6.no){
                break;
            }
            temp = temp.next;
        }
        //判断是否可以添加改编号的数据
        if (flag){
            System.out.printf("不能添加编号 %d 的节点，因其已存在与链表中",hero6.no);
        }else {

            System.out.printf("加入%d\n",hero6.no);
            hero6.next = temp.next;
            temp.next = hero6;
        }
    }
}
class HeroNode2{
    public int no;
    public String name;
    public String nickname;
    public HeroNode2 pre;//指向上一个节点
    public HeroNode2 next;//指向下一个节点

    //构造器
    public HeroNode2(int no, String name, String nickname) {
        this.no = no;
        this.name = name;
        this.nickname = nickname;
    }

    @Override
    public String toString() {
        return "HeroNode2[no="+no+",name="+name+",nickname= "+nickname+"]";
    }
}
